import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login.vue'
import Main from '@/views/Main/Main.vue'
import store from '@/store'

import Home from '@/views/menu/Home/Home.vue'
import UserAvatar from '@/views/menu/User/UserAvatar.vue'
import UserInfo from '@/views/menu/User/UserInfo.vue'
import UserPwd from '@/views/menu/User/UserPwd.vue'
import ArtCate from '@/views/menu/Article/ArtCate.vue'
import ArtList from '@/views/menu/Article/ArtList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      { path: 'home', component: Home },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-info', component: UserInfo },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList }

    ]
  },
  { path: '/reg', component: Reg },
  { path: '/login', component: Login }
]

const router = new VueRouter({
  routes
})

// 前置守卫
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  // console.log(token)
  const whiteList = ['/login', '/reg']
  // console.log(whiteList)
  if (!token && !whiteList.includes(to.path)) {
    next('/login')
  } else {
    next()
  }
})
export default router
